package javaCode.chapter12.stringbuilder_;

/**
 * @author 徐州城
 * @version 1.0
 */
public class StringVsStringBufferVsStringBuilder {
    public static void main(String[] args) {
        //比较三者效率
        //StringBuffer
        implementTime1 time1 = new implementTime1();
        time1.calcTime();

        //StringBuilder
        implementTime2 time2 = new implementTime2();
        time2.calcTime();

        //String
        implementTime3 time3 = new implementTime3();
        time3.calcTime();
    }
}

abstract class Time_{ //模板设计模式
    public abstract void job();

    public void calcTime(){
        long startTime = 0;
        long endTime = 0;
        startTime = System.currentTimeMillis();
        job();
        endTime = System.currentTimeMillis();
        System.out.println("执行时间：" + (endTime - startTime));
    }
}

class implementTime1 extends Time_{
    @Override
    public void job() {
        StringBuffer buffer = new StringBuffer("");
        for (int i = 0; i < 20000; i++) {
            buffer.append(String.valueOf(i));
        }
    }
}

class implementTime2 extends Time_{
    @Override
    public void job() {
        StringBuilder builder = new StringBuilder("");
        for (int i = 0; i < 20000; i++) {
            builder.append(i);
        }
    }
}

class implementTime3 extends Time_{
    @Override
    public void job() {
        String text = "";
        for (int i = 0; i < 20000; i++) {
            text = text + i;
        }
    }
}